package com.kadron.order;

import java.util.List;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.creator.KOrderModelCreator;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KOrderDAO;
import com.kadron.dao.KOrderItemDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KOrderDataProvider;
import com.kadron.entity.Currency;
import com.kadron.enumeration.OrderStatus;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name(KOrderManager.K_NAME)
public class KOrderManager extends KMessageManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2240644824574701293L;
	
	public static final String K_NAME = "kOrderManager";

	private KDataModel orderList;
	private KOrderDataProvider dataProvider;
	private SearchOrder search;
	private List<Currency> currencies;
	private OrderStatus[] orderStatuses = OrderStatus.values();
	
	@In
	private KOrderDAO orderDAO;
	@In
	private KOrderItemDAO orderItemDAO;
	@In
	private KImageDAO imageDAO;
	
	@Create
	public void init() {
		currencies = orderDAO.getAll(Currency.class);
		search = new SearchOrder();
		dataProvider = new KOrderDataProvider(search,new KOrderModelCreator(orderDAO, orderItemDAO, imageDAO), orderDAO);
		orderList = new KDataModel(dataProvider);
	}
	
	public void search() {
		if (dataProvider == null) {
			dataProvider = new KOrderDataProvider(search,new KOrderModelCreator(orderDAO, orderItemDAO, imageDAO), orderDAO);			
		} else {
			dataProvider.setSearch(search);
		}
		orderList = new KDataModel(dataProvider);		
	}
	
	public KDataModel getOrderList() {
		return orderList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_order";
	}
	
	public SearchOrder getSearch() {
		return search;
	}

	public List<Currency> getCurrencies() {
		return currencies;
	}

	public OrderStatus[] getOrderStatuses() {
		return orderStatuses;
	}
}
